const INFO_KEY = 'hm_shopping_info'
const HISTORY_KEY = 'hm_history_list'

// 获取个人信息
export const getInfo = () => {
  const defaultObj = { token: '', userId: '' }
  try {
    const result = localStorage.getItem(INFO_KEY)
    return result ? JSON.parse(result) : defaultObj
  } catch (error) {
    console.error('Failed to retrieve info from localStorage:', error)
    return defaultObj
  }
}

// 设置个人信息
export const setInfo = (obj) => {
  try {
    localStorage.setItem(INFO_KEY, JSON.stringify(obj))
  } catch (error) {
    console.error('Failed to save info to localStorage:', error)
  }
}

// 移除个人信息
export const removeInfo = () => {
  try {
    localStorage.removeItem(INFO_KEY)
  } catch (error) {
    console.error('Failed to remove info from localStorage:', error)
  }
}

// 获取搜索历史
export const getHistoryList = () => {
  try {
    const result = localStorage.getItem(HISTORY_KEY)
    return result ? JSON.parse(result) : []
  } catch (error) {
    console.error('Failed to parse history from localStorage:', error)
    return []
  }
}

// 设置搜索历史
export const setHistoryList = (arr) => {
  try {
    localStorage.setItem(HISTORY_KEY, JSON.stringify(arr))
  } catch (error) {
    console.error('Failed to save history to localStorage:', error)
  }
}
